// ignore_for_file: unused_local_variable

import 'package:flutter/material.dart';
import 'package:get/get.dart';
import '../../app_views.dart';
import 'root_state.dart';

/// 根页面逻辑控制器 - Root Logic Controller
///
/// 管理底部导航的选中状态和页面切换逻辑
/// Manages bottom navigation selection state and page switching logic
///
/// 基于GetX最新最佳实践实现，提供响应式状态管理
/// Based on GetX latest best practices, providing reactive state management
class RootLogic extends GetxController {
  /// 状态管理 - State management
  final RootState state = RootState();

  @override
  void onInit() {
    super.onInit();
    // 初始化状态 - Initialize state
    _initializeState();
    update();
  }

  /// 初始化状态 - Initialize state
  void _initializeState() {
    state.currentIndex = 0;
    state.isLoading = false;
    state.errorMessage = null;
  }

  /// 处理标签页选中事件 - Handle tab selection event
  /// 当用户点击底部导航栏时调用
  /// Called when user taps bottom navigation bar
  void onTabSelected(int index) {
    if (index < 0 || index >= state.navItems.length) {
      debugPrint('无效的标签页索引: $index');
      return;
    }

    try {
      // 更新当前索引 - Update current index
      state.currentIndex = index;
      update();
      state.errorMessage = null;
      update();
      // 根据选中的页面执行特定逻辑 - Execute specific logic based on selected page
      _handlePageSpecificLogic(index);

      debugPrint('切换到页面: ${state.navItems[index].title} (索引: $index)');
    } catch (e) {
      state.errorMessage = '页面切换失败: $e';
      debugPrint('页面切换失败: $e');
    }
  }

  /// 处理页面特定逻辑 - Handle page specific logic
  /// 根据不同页面执行相应的初始化或刷新逻辑
  /// Execute corresponding initialization or refresh logic for different pages
  void _handlePageSpecificLogic(int index) {
    switch (index) {
      case 0: // 首页 - Home
        _refreshHomePage();
        break;
      case 1: // 万年历 - Calendar
        _refreshCalendarPage();
        break;
      case 2: // 课堂 - Classroom
        _refreshClassroomPage();
        break;
      case 3: // 商城 - Store
        _refreshStorePage();
        break;
      case 4: // 我的 - Profile
        _refreshProfilePage();
        break;
      default:
        debugPrint('未知页面索引: $index');
    }
  }

  /// 刷新首页数据 - Refresh home page data
  void _refreshHomePage() {
    try {
      if (Get.isRegistered<HomeLogic>()) {
        final homeLogic = Get.find<HomeLogic>();
        // 可以选择性地刷新首页数据 - Optionally refresh home page data
        // homeLogic.refreshAllData();
      }
    } catch (e) {
      debugPrint('刷新首页失败: $e');
    }
  }

  /// 刷新万年历页面数据 - Refresh calendar page data
  void _refreshCalendarPage() {
    try {
      if (Get.isRegistered<CalendarLogic>()) {
        final calendarLogic = Get.find<CalendarLogic>();
        // 可以选择性地刷新万年历数据 - Optionally refresh calendar data
        // calendarLogic.refresh();
      }
    } catch (e) {
      debugPrint('刷新万年历页面失败: $e');
    }
  }

  /// 刷新课堂页面数据 - Refresh classroom page data
  void _refreshClassroomPage() {
    try {
      if (Get.isRegistered<ClassroomLogic>()) {
        final classroomLogic = Get.find<ClassroomLogic>();
        // 可以选择性地刷新课堂数据 - Optionally refresh classroom data
        // classroomLogic.loadCourses();
      }
    } catch (e) {
      debugPrint('刷新课堂页面失败: $e');
    }
  }

  /// 刷新商城页面数据 - Refresh store page data
  void _refreshStorePage() {
    try {
      if (Get.isRegistered<StoreLogic>()) {
        final storeLogic = Get.find<StoreLogic>();
        // 可以选择性地刷新商城数据 - Optionally refresh store data
        // storeLogic.loadProducts();
      }
    } catch (e) {
      debugPrint('刷新商城页面失败: $e');
    }
  }

  /// 刷新个人中心页面数据 - Refresh profile page data
  void _refreshProfilePage() {
    try {
      if (Get.isRegistered<ProfileLogic>()) {
        final profileLogic = Get.find<ProfileLogic>();
        // 刷新用户信息 - Refresh user information
        profileLogic.loadUserInfo();
      }
    } catch (e) {
      debugPrint('刷新个人中心页面失败: $e');
    }
  }

  /// 获取当前页面标题 - Get current page title
  String getCurrentPageTitle() {
    final index = state.currentIndex;
    update();
    if (index >= 0 && index < state.navItems.length) {
      return state.navItems[index].title;
    }
    return '未知页面';
  }

  /// 程序化切换页面 - Programmatically switch page
  /// 用于其他页面需要切换到特定标签页时调用
  /// Called when other pages need to switch to specific tab
  void switchToPage(int index) {
    onTabSelected(index);
  }

  /// 切换到首页 - Switch to home page
  void switchToHome() => switchToPage(0);

  /// 切换到万年历页面 - Switch to calendar page
  void switchToCalendar() => switchToPage(1);

  /// 切换到课堂页面 - Switch to classroom page
  void switchToClassroom() => switchToPage(2);

  /// 切换到商城页面 - Switch to store page
  void switchToStore() => switchToPage(3);

  /// 切换到个人中心页面 - Switch to profile page
  void switchToProfile() => switchToPage(4);
}
